package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.TCar;
import com.zr.model.TCarstate;
import com.zr.service.CarService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 车辆的控制层
 * @author Administrator
 * 
 */
@Controller("carAction")
// 保证当有请求的时候,都创建一个Action对象
@Scope(value="prototype")
public class CarAction extends ActionSupport implements ServletResponseAware,ServletRequestAware{
	private HttpServletResponse response;
	private int page;
	private int rows;
	private String json;
	private HttpServletRequest request;
	private int cids[];
	private String json1;
	public String getJson1() { return json1; }
	public void setJson1(String json1) { this.json1 = json1; }
	public int[] getCids() {return cids;}
	public void setCids(int[] cids) {this.cids = cids;}
	public String getJson() { return json;	}
	public void setJson(String json) {	this.json = json; }
	public int getPage() { 	return page;  }
	public void setPage(int page) { 	this.page = page; }
	public int getRows() {	return rows; }
	public void setRows(int rows) {  this.rows = rows; }
	@Resource
	private  CarService  carService;
	//展示车辆信息
	public void showCarMsg(){
		JSONObject obj = carService.showCarMsg(page, rows);
		response.setCharacterEncoding("utf-8");
		try {
			response.getWriter().write(obj.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//展示车辆状态信息
	public void findCarCsnameMsg(){
		JSONArray obj = carService.findCarCsnameMsg();
		response.setCharacterEncoding("utf-8");
		try {
			response.getWriter().write(obj.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//添加车辆信息
	public void addCarMsg(){
		JSONObject jso = JSONObject.fromObject(json);
		int csid=jso.getInt("csid");
		String cname=jso.getString("cname");
		String cvolume=jso.getString("cvolume");
		Boolean flag=carService.addCarMsg(cname, cvolume, csid);
		response.setCharacterEncoding("utf8");
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.write(flag+"");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//删除车辆信息
	public void deleteCarMsg(){
		String cids[]=request.getParameterValues("cids[]");
		carService.deleteCarMsg(cids);
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.write("");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	//修改车辆信息
	public void updateCarMsg(){
		JSONObject jso = JSONObject.fromObject(json1);
		int csid=jso.getInt("csid");
		int cid = jso.getInt("cid");
		String cname=jso.getString("cname");
		String cvolume=jso.getString("cvolume");
		carService.updateCarMsg(cid, cname, cvolume, csid);
		response.setCharacterEncoding("utf8");
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.write("");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request=request;
	}
}
